(function(){
	
	// 验证
	if(!localStorage.getItem('token')){
		alert('非法登录！');
		location.href = 'index.html';
		return;
	};
	
	
	// 获取订单号
	let tradeNo = lb.getUrlValue('trade_no');

	// 查询支付结果
	lb.ajax({
		method: 'post',
		url: BASE_URL + '/api_payquery',
		data: {
			userId: localStorage.getItem('token'),
			tradeNo,
		},
		success(res){
			if(res.code != 0){
				console.log(res);
				return;
			};
			// 查询成功，获取用户订单列表
			getOrderList();
		}
	});
	
	// 页面刷新获取订单列表
	getOrderList();
	
	// 订单列表
	function getOrderList(){
		let oOrderList = document.querySelector('.order-list');
		
		oOrderList.innerHTML = '';
		
		lb.ajax({
			method: 'post',
			url: BASE_URL + '/api_order',
			data: {
				status: 'vieworder',
				userId: localStorage.getItem('token')
			},
			success(res){
				if(res.code != 0){
					console.log(res);
					return;
				};
				console.log(res);
				// DOM
				let orderData = res.data;
				
				orderData.forEach(item => {
					
					let strLi = '';
					let priceAll = 0;
					let strGoods = `
						<div class="goods-list">
							<img src="" alt="" />
							<span>商品名</span>
							<span>单价</span>
							<span>数量</span>
							<span>总价</span>
						</div>
					`;
					// 商品遍历
					item.goods.forEach(goodsItem => {
						strGoods += `
							<div class="goods-list">
								<img src="${goodsItem.goods_thumb}" alt="" />
								<span>${goodsItem.goods_name}</span>
								<span>${goodsItem.price}</span>
								<span>${goodsItem.goods_number}</span>
								<span>${parseInt(goodsItem.goods_number) * goodsItem.price}</span>
							</div>
						`;
						priceAll += parseInt(goodsItem.goods_number) * goodsItem.price;
					});
					// <div class="addrsss">${JSON.stringify(item.order_address)}</div>
					// console.log(item.order_address);
					// console.log(JSON.stringify(item.order_address));
					strLi += strGoods;
					strLi += `
						<div class="order-operate">
							<div class="order-id">订单号：${item.trade_no}</div>
							
							<div class="price-all">总价：￥${priceAll}</div>
							<div class="order-status">支付状态：${item.status}</div>
							<div class="del-order" trade-no="${item.trade_no}">删除订单</div>
						</div>
					`;
					
					let li = document.createElement('li');
					li.innerHTML = strLi;
					oOrderList.appendChild(li);
				});
				
				// 调用头部购物车渲染方法
				getCartValue();
				
				// 选择删除按钮
				let aDelBtn = document.querySelectorAll('.order-list .del-order');
				aDelBtn.forEach(item => {
					
					item.onclick = function(){
						lb.ajax({
							method: 'post',
							url: BASE_URL + '/api_order',
							data: {
								status: 'delorder',
								userId: localStorage.getItem('token'),
								tradeNo: this.getAttribute('trade-no')
							},
							success: res => {
								console.log(res);
								if(res.code != 0){
									console.log(res);
									return;
								};
								
								this.parentNode.remove();
							}
						})
						getOrderList();
					};
				})
			},
			
		});
	};
	
})();